package com.cnzz.site1255174697.activity;

import android.app.Fragment;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RadioGroup;

import com.ali.auth.third.core.trace.SDKLogger;
import com.ali.auth.third.login.LoginComponent;
import com.ali.auth.third.ui.context.CallbackContext;
import com.alibaba.baichuan.android.trade.adapter.login.AlibcLogin;
import com.cnzz.site1255174697.Const;
import com.cnzz.site1255174697.R;
import com.cnzz.site1255174697.activity.user.LoginActivity;
import com.cnzz.site1255174697.fragment.main.BigFragment;
import com.cnzz.site1255174697.fragment.main.CartFragment;
import com.cnzz.site1255174697.fragment.main.IndexFragment;
import com.cnzz.site1255174697.fragment.main.KindsFragment;
import com.cnzz.site1255174697.fragment.main.MyFragment;
import com.cnzz.site1255174697.utils.AlibcTradeUtils;
import com.cqyanyu.yanyu.activity.XActivity;
import com.cqyanyu.yanyu.model.EventEntity;
import com.cqyanyu.yanyu.model.UserInfoEntity;
import com.cqyanyu.yanyu.utils.StatusBarUtil;
import com.cqyanyu.yanyu.utils.XToastUtil;
import com.cqyanyu.yanyu.view.sliding.XBottomTabView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * 主界面
 */
@ContentView(R.layout.activity_main)
public class MainActivity extends XActivity {
    @ViewInject(R.id.mBottomTabView)
    protected XBottomTabView mBottomTabView;
    private long exitTime = 0;
    private CartFragment cartFragment;
    private MyFragment myFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setStatusBarColor(this, R.color.transparent);
        initView();
        EventBus.getDefault().register(this);
    }

    private void initView() {
        List<Fragment> mFragments = new ArrayList<Fragment>();
        mFragments.add(new IndexFragment());
        mFragments.add(new KindsFragment());
        mFragments.add(new BigFragment());
        cartFragment = new CartFragment();
        mFragments.add(cartFragment);
        myFragment = new MyFragment();
        mFragments.add(myFragment);
        List<String> tabTexts = new ArrayList<String>();
        tabTexts.add(getString(R.string.home));
        tabTexts.add(getString(R.string.kinds));
        tabTexts.add(getString(R.string.big));
        tabTexts.add(getString(R.string.shop_cart));
        tabTexts.add(getString(R.string.my_taobao));

        //设置样式
        mBottomTabView.setTabTextColor(getResources().getColor(R.color.tab_text));
        mBottomTabView.setTabSelectColor(getResources().getColor(R.color.red));
        mBottomTabView.setTabBackgroundResource(Color.TRANSPARENT);
        mBottomTabView.setTabLayoutBackgroundResource(R.drawable.bottom_bar_bg);
        mBottomTabView.setTabSlidingColor(Color.TRANSPARENT);
        mBottomTabView.setTabTextColor(getResources().getColor(R.color.textTintGray));
        mBottomTabView.setTabTextSize(22);

        //注意图片的顺序
        ArrayList<Drawable> tabDrawables = new ArrayList<Drawable>();
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.shouye));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.shouyeb));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.fenlei));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.fenleib));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.dapai));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.pinpaib));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.gwc));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.gwcb));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.gerenzhongxin));
        tabDrawables.add(this.getResources().getDrawable(R.mipmap.gerenb));

        mBottomTabView.setTabCompoundDrawablesBounds(0, 2, 44, 44);
        //演示增加一组
        mBottomTabView.addItemViews(tabTexts, mFragments, tabDrawables);
        mBottomTabView.setTabPadding(10, 10, 10, 10);
        mBottomTabView.getViewPager().setOffscreenPageLimit(3);
        mBottomTabView.getViewPager().setAdapter(mBottomTabView.getViewPager().getAdapter());
        mBottomTabView.setSlidingEnabled(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(EventEntity eventEntity) {
        if (eventEntity != null && eventEntity.type == Const.JUMP_KINDS) {
            mBottomTabView.setCurrentItem(1);
        } else if (eventEntity.type == Const.JUMP_CART) {
            mBottomTabView.setCurrentItem(3);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                XToastUtil.showToast(this, "再按一次就退出了");
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                Intent home = new Intent(Intent.ACTION_MAIN);
                home.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                home.addCategory(Intent.CATEGORY_HOME);
                startActivity(home);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

}
